<?php
defined('IN_IXDCW') or exit('Access Denied');

class OwnersModule extends BasicModule {

	/** @var 模块id */
	private $_moduleid;

	/** @var 用户名 */
	private $_username;

	/** @var 条目ID */
	private $_itemid;

	/** @var string 表格 */
	private $_table;

	/** @var array 可用字段 */
	private $_fields;

	/**
	 * constructor
	 * @param $moduleid
	 */
	function __construct($moduleid) {
		parent::__construct();
		global $table, $MOD,$moduleid;
		$this->_moduleid  = $moduleid;
		$this->_table     = $table;
		$this->_table_like = $table.'_like';
		$this->_fields_like     = array('acid','arbid','arcid','amid','username','addtime','edittime');
		$this->_fields = array('areaid','zuobiao','acid','arbid','arcid','amid','bytime','username','edittime');
    }
	public function setUsername($_username) {
		$this->_username = $_username;
	}

	public function setitemid($_itemid) {
		$this->_itemid = $_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		if(!is_array($post)) return false;
		// if(empty($post['arbid'][0])) return $this->err(lang('message->pass_arbid'));
		return true;
	}

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function passArea($post) {
		if(!is_array($post)) return false;
		if(!$post['zuobiao']) return $this->err('请在地图上标记您的位置');
		return true;
	}

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function passlike($post) {
		if(!is_array($post)) return false;
		// if(empty($post['arbid'][0])) return $this->err(lang('message->pass_arbid'));
		return true;
	}

	/**
	 * 设置符合内容的值
	 * @param $post
	 * @return array|mixed
	 */
	function set($post) {
		global $MOD, $DT_TIME,$_username;
		$post['username'] = $_username;
		$post['addtime']  = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
		$post['edittime'] = $DT_TIME;
		if($post['areaid'] )$post['areaid']  = intval(Area::area2id($post['areaid'],' '));
		if($post['arbid']){
		$car              = AutoInfo::byAcid($post['arbid'],$post['arcid'],$post['amid']);
		$post['acid']     = $car['acid'];
		$post['arbid']    = $car['arbid'];
		$post['arcid']    = $car['arcid'];
		$post['amid']     = $car['amid'];
		}
		return $post;
	}

	/**
	 * 获取一条数据
	 * @return array|void
	 */
	function getOne() {
        return $this->_db->getOne("SELECT {$this->_table}.* FROM {$this->_table} WHERE  {$this->_table}.username='".$this->_username."'");
	}

	/**
	 * 获取一条用户所在地区车主数据
	 * @param $modelid
	 * @return array|void
	 */
	function getOneArea($modelid) {
		global $AREA;
		$pos = array();
		$userInfo = $this->_db->getOne("SELECT {$this->_table}.areaid FROM {$this->_table} WHERE {$this->_table}.username='".$this->_username."'");
		$arrparentid   = $AREA[$userInfo['areaid']]['arrparentid'] ? explode(',', $AREA[$userInfo['areaid']]['arrparentid']) : array();
		$arrparentid[] = $userInfo['areaid'];
		foreach ($arrparentid as $k=>$areaid) {
			if (!$areaid || !isset($AREA[$areaid])) continue;
			if($AREA[$areaid]['areaname'] == '国内'){
				$pos[$k]['areaname'] ='全国';
			 }else{
				$pos[$k]['areaname'] = $AREA[$areaid]['areaname'];
			 }
			$pos[$k]['areaid']   = $areaid;
			$pos[$k]['areanum']  = $this->_db->getVar("SELECT COUNT(*) FROM {$this->_table} WHERE areaid in ({$AREA[$areaid]['arrchildid']}) and amid like '%".$modelid."%'");
		}
		return $pos;
	}

	/**
	 * 获取一条关注车型数据
	 * @return array|void
	 */
	function getOne_likecar() {
		return $this->_db->getOne("SELECT * FROM {$this->_table_like}  WHERE itemid='".$this->_itemid."'");
	}


	/**
	 * 获取用户关注车型数据
	 * @param string $condition
	 * @param string $order
	 * @return array
	 */
	function getList_likecar($condition = '',$order='') {
		$num = $this->_db->count($this->_table_like, $condition."username ='".$this->_username."'");
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table_like} ";
		$sql .= " where username ='".$this->_username."'";
		$sql .= $condition ? " and  {$condition} " : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql);
		while($r = $this->_db->fetchArray($result)) {
			$r['brand'] = AutoBrand::get($r['arbid']);
			$r['car'] = AutoCar::get($r['arcid']);
			$r['model'] = AutoModel::get($r['amid']);
			$lists[] = $r;

		}
		return $lists;
	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $pagesize
	 * @return array
	 */
	function getList($condition = '',$order='',$pagesize = '') {
		$MOD = PCache::read('module-2.php');
		require DT_ROOT.'/api/uc.inc.php';
		unset($MOD);
		global $items,$AREA;
		if(!$pagesize){$pagesize = $this->_pagesize;}
		$num = $this->_db->count($this->_table, $condition);
		$items = $num;
		$this->_pn->recordsPerPage($pagesize);
		$this->_pn->records($num);
		$sql = "SELECT {$this->_table}.*,xd_member.userid,xd_member.truename FROM {$this->_table},xd_member ";
		$sql .= " where {$this->_table}.username = xd_member.username ";
		$sql .= $condition ? " and  {$condition} " : '';
		$sql .= $order ? " ORDER BY {$this->_table}.{$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $pagesize . ',' . $pagesize;
		$lists = $catids = $CATS  = array();
		$result = $this->_db->query($sql);
		while($r = $this->_db->fetchArray($result)) {
			$r['likecar'] = $this->_db->getALL("select * from {$this->_table_like} where username = '".$r['username']."' order by addtime desc limit 5");
			foreach($r['likecar'] as $k=>$v){
				$r['likecar'][$k]['car'] = AutoCar::get($v['arcid']);
				$r['likecar'][$k]['model'] = AutoModel::get($v['amid']);
			}
			$r['areaname']     = explode('|',area_pos($r['areaid'],'|'));
			$r['editdate']   = timetodate($r['edittime'], 5);
			$r['thumb']      = UC_API.'/avatar.php?uid='.$r['userid'];
			$r['arcid'] = explode(',',$r['arcid']);
			foreach($r['arcid'] as $k=>$v){
			   $r['carinfo'][$k]    = AutoCar::get($v);

			}
			$r['amid'] = explode(',',$r['amid']);
			foreach($r['amid'] as $k=>$v){
				$r['modelinfo'][$k]  = AutoModel::get($v);
			}
			$lists[] = $r;

		}
		return $lists;
	}

	/**
	 * 添加关注车型
	 * @param $post
	 */
	function addcar($post) {
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table_like, $post, $this->_fields_like));
		return $this->_itemid;
	}

	/**
	 * 编辑
	 * @param $post
	 * @return bool
	 */
	function edit($post) {
		$post = $this->set($post);
		$item = $this->getOne();
		if(!$item){
			$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
			return $this->_username;
		}else{
			if($post['arbid'] && $item['arbid']){
				$post['acid']     = $item['acid'].'|'.$post['acid'];
				$post['arbid']    = $item['arbid'].','.$post['arbid'];
				$post['arcid']    = $item['arcid'].','.$post['arcid'];
				$post['amid']     = $item['amid'].','.$post['amid'];
				$post['bytime']     = $item['bytime'].','.$post['bytime'];
			}
			$this->_db->query(Sql::update($this->_table, $post, "username='".$this->_username."'", '', $this->_fields));
			return true;
		}
	}

	/**
	 * 编辑关注车型
	 * @param $post
	 * @return bool
	 */
	function editcar($post) {
		//$this->delete($this->_itemid, false);
		$post = $this->set($post);
		$this->_db->query(Sql::update($this->_table_like, $post, "itemid={$this->_itemid}", '', $this->_fields_like));
		return true;
	}



	/**
	 * 删除关注车型
	 * @param      $itemid
	 * @param bool $all
	 */
	function delcar($itemid, $all = true ) {
		if(is_array($itemid)) {
			foreach($itemid as $v) {
				$this->delcar($v, $all);
			}
		}
		$this->_db->query(Sql::delete($this->_table_like,"itemid=$itemid"));
	}

	/**
	 * 删除爱车车型
	 * @param      $itemid
	 * @param      $post
	 */
	function editlove($itemid, $post ) {
		$post = $this->set($post);
		$item = $this->getOne();
		$item['arbid'] = explode(',',$item['arbid']);
		$item['arcid'] = explode(',',$item['arcid']);
		$item['amid'] = explode(',',$item['amid']);
		$item['acid'] = explode('|',$item['acid']);
		$item['bytime'] = explode(',',$item['bytime']);

		foreach($item['arbid'] as $k=>$v){
			if($k==($itemid-1)){
				$item['arbid'][$k] = $post['arbid'];
				$item['acid'][$k]  = $post['acid'];
				$item['arcid'][$k] = $post['arcid'];
				$item['amid'][$k]  = $post['amid'];
				$item['bytime'][$k] = $post['bytime'];
			}
		}
		$post['arbid'] = implode(',',$item['arbid']);
		$post['arcid'] = implode(',',$item['arcid']);
		$post['amid'] = implode(',',$item['amid']);
		$post['bytime'] = implode(',',$item['bytime']);
		$post['acid'] = implode('|',$item['acid']);
		$this->_db->query(Sql::update($this->_table, $post, "username='".$this->_username."'", '', $this->_fields));
	}


	/**
	 * 删除爱车车型
	 * @param      $itemid
	 * @param bool $all
	 */
	function delete($itemid, $all = true ) {
		if(is_array($itemid)) {
			foreach($itemid as $v) {
				$this->delete($v, $all);
			}
		}
		$item = $this->getOne();
		$item['arbid'] = explode(',',$item['arbid']);
		$item['arcid'] = explode(',',$item['arcid']);
		$item['amid'] = explode(',',$item['amid']);
		$item['acid'] = explode('|',$item['acid']);
		$item['bytime'] = explode(',',$item['bytime']);

		foreach($item['arbid'] as $k=>$v){
			if($k==($itemid-1)){
				unset($item['arbid'][$k]);
				unset($item['acid'][$k]);
				unset($item['arcid'][$k]);
				unset($item['amid'][$k]);
				unset($item['bytime'][$k]);
			}
		}
		$post['arbid'] = implode(',',$item['arbid']);
		$post['arcid'] = implode(',',$item['arcid']);
		$post['amid'] = implode(',',$item['amid']);
		$post['bytime'] = implode(',',$item['bytime']);
		$post['acid'] = implode('|',$item['acid']);
		$this->_db->query(Sql::update($this->_table, $post, "username='".$this->_username."'", '', $this->_fields));
	}

}